package com.ruoyi.common.utils.word;
import com.kennycason.kumo.nlp.FrequencyAnalyzer;
import com.kennycason.kumo.WordFrequency;
import com.kennycason.kumo.nlp.tokenizers.ChineseWordTokenizer;
import java.util.List;

/**
 * @version: java version 1.8
 * @Author: Aous Volad
 * @description:
 * @date: 2024-07-05 9:47
 */
public class WordUtil {
    public static List<WordFrequency> wordCloud (List<String> text)  {

        // 新建FrequencyAnalyzer 对象（这块是强引用，并发的情况下会造成oom！！！改为弱引用即可）
        FrequencyAnalyzer frequencyAnalyzer = new FrequencyAnalyzer();
        // 设置分词返回数量(频率最高的600个词)
        frequencyAnalyzer.setWordFrequenciesToReturn(600);
        // 最小分词长度
        frequencyAnalyzer.setMinWordLength(2);
        // 引入中文解析器
        frequencyAnalyzer.setWordTokenizer(new ChineseWordTokenizer());
        // 通过String List统计词频
        List<WordFrequency> wordFrequencies = frequencyAnalyzer.load(text);
        return wordFrequencies;
    }
}
